import React, { useEffect, useState } from "react";
import PubSub from "pubsub-js";

export default function C() {
  //定义状态
  let [data, setData] = useState("");
  //定义 useEffect 回调  
  useEffect(() => { 
    //订阅
    PubSub.subscribe("text", (name, data) => {
      console.log(data);
      //更新状态
      setData(data);
    });

    //组件销毁前取消订阅
    return () => {
      PubSub.unsubscribe("text");
    };
  }, []);

  return (
    <div>
      <div>C</div>
      <div style={{ width: 300, height: 100, border: "solid 1px #000" }}>
        {data}
      </div>
    </div>
  );
}
